require 'View.Base.BehaviourView'

UIDark = class("UIDark", BehaviourView)

function UIDark:Awake(obj)
    BehaviourView.Awake(self, obj)
    self.colorCacheDict = {} --key=instanceId vale=color
    self.imgList = {}
    self.textList = {}
    self.rawImgList = {}
    self.drakPercentage = 0.6

    self:initImage()
    self:initText()
end
function UIDark:initImage()
    local _addFunc = function(img)
        local instanceId = img:GetInstanceID()
        self.colorCacheDict[instanceId] = img.color
    end
    local imgList = self:getComponents(typeof(UnityEngine.UI.Image))
    for i=1,#imgList do
        local img = imgList[i]
        table.insert(self.imgList, img)
        _addFunc(img)
    end

    local rawImgList = self:getComponents(typeof(UnityEngine.UI.RawImage))
    for i=1,#rawImgList do
        local img = rawImgList[i]
        table.insert(self.rawImgList, img)
        _addFunc(img)
    end
end

function UIDark:initText()
    local _addFunc = function(text)
        local instanceId = text:GetInstanceID()
        self.colorCacheDict[instanceId] = text.color
    end
    local textList = self:getComponents(typeof(UnityEngine.UI.Text))
    for i=1,#textList do
        local text = textList[i]
        table.insert(self.textList, text)
        _addFunc(text)
    end
end

function UIDark:getComponents(compType)
    local ret = {}
    local comp = self.transform:GetComponent(compType)
    if img then
        table.insert(ret, comp)
    end
    local compList = self.transform:GetComponentsInChildren(compType)
    for i=1,compList.Length do
        local comp = compList[i-1]
        table.insert(ret, comp)
    end
    return ret
end


function UIDark:OnShow()
    for _, img in ipairs(self.imgList) do
        local instanceId = img:GetInstanceID()
        local cache = self.colorCacheDict[instanceId]
        img.color = Color.New(cache.r*self.drakPercentage, cache.g*self.drakPercentage, cache.b*self.drakPercentage, cache.a)
    end
    for _, img in ipairs(self.rawImgList) do
        local instanceId = img:GetInstanceID()
        local cache = self.colorCacheDict[instanceId]
        img.color = Color.New(cache.r*self.drakPercentage, cache.g*self.drakPercentage, cache.b*self.drakPercentage, cache.a)
    end
    for _, text in ipairs(self.textList) do
        local instanceId = text:GetInstanceID()
        local cache = self.colorCacheDict[instanceId]
        text.color = Color.New(cache.r*self.drakPercentage, cache.g*self.drakPercentage, cache.b*self.drakPercentage, cache.a)
    end
end
function UIDark:OnHide()
    for _, img in ipairs(self.imgList) do
        local instanceId = img:GetInstanceID()
        local cache = self.colorCacheDict[instanceId]
        img.color = cache
    end
    for _, img in ipairs(self.rawImgList) do
        local instanceId = img:GetInstanceID()
        local cache = self.colorCacheDict[instanceId]
        img.color = cache
    end
    for _, text in ipairs(self.textList) do
        local instanceId = text:GetInstanceID()
        local cache = self.colorCacheDict[instanceId]
        text.color = cache
    end
end
